package top.ysh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ysh.common.Code;
import top.ysh.common.Result;
import top.ysh.domain.User;
import top.ysh.service.UserService;

import java.util.List;

/**
 * 用户表现层
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result<User> getAll(){
        List<User> users = userService.findAll();
        return new Result(Code.OK,"查询成功",users);
    }

    /**
     * 新增用户信息
     * @param user
     * @return
     */
    @PostMapping
    public Result<User> save(@RequestBody User user){
        boolean result = userService.save(user);
        return new Result<User>(result ? Code.OK : Code.ERROR, result?"新增成功":"新增失败");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping
    public Result<User> update(@RequestBody User user){
        boolean update = userService.update(user);
        return new Result<User>(update ? Code.OK : Code.ERROR, update?"修改成功":"修改失败");
    }

    /**
     * 删除用户
     * @param uuid
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<User> delete(@PathVariable("id") Integer uuid){
        boolean delete = userService.delete(uuid);
        return new Result<User>(delete ? Code.OK : Code.ERROR, delete?"删除成功":"删除失败");
    }

}
